<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>购物车</title>
    <style>
        div {
            position: absolute;
            top: 30%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
        }

        table, tr, th, td {
            border: 1px solid #5fbdcb;
        }

        table {
            border-collapse: collapse;
        }
    </style>
</head>
<body>
<div id="div1">
</div>
<script src="https://lib.sinaapp.com/js/jquery/2.0.2/jquery-2.0.2.min.js"></script>
<script>
    $(function () {
        $.ajax({
            url: "http://localhost:8080/cart.do",
            type: "post",
            data: {
                method: 'selectCart'
            },
            dataType: "text",
            success: function (data) {
                console.log(data);
                let parse = JSON.parse(data);
                console.log(parse);
                if (parse.length === 0) {
                    alert("购物车空空如也，请先添加！");
                    window.location.href = 'UserPage.jsp';
                } else {
                    let div = document.querySelector('#div1');
                    for (let i = 0; i < parse.length; i++) {
                        let money = 0;
                        let table = document.createElement('table');
                        div.appendChild(table);
                        let tr1 = document.createElement('tr');
                        table.appendChild(tr1);
                        let th1 = document.createElement('th');
                        th1.innerText = '店铺名：';
                        tr1.appendChild(th1);
                        let td1 = document.createElement('td');
                        td1.innerText = parse[i][0].storeName;
                        tr1.appendChild(td1);
                        for (let j = 0; j < parse[i].length; j++) {
                            let tr = document.createElement('tr');
                            table.appendChild(tr);
                            let th1 = document.createElement('th');
                            th1.innerText = '菜品名：';
                            tr.appendChild(th1);
                            let td1 = document.createElement('td');
                            td1.innerText = parse[i][j].dishName;
                            tr.appendChild(td1);
                            let th2 = document.createElement('th');
                            th2.innerText = '价格：';
                            tr.appendChild(th2);
                            let td2 = document.createElement('td');
                            td2.innerText = parse[i][j].price;
                            tr.appendChild(td2);
                            let th3 = document.createElement('th');
                            th3.innerText = '描述：';
                            tr.appendChild(th3);
                            let td3 = document.createElement('td');
                            td3.innerText = parse[i][j].describe;
                            tr.appendChild(td3);
                            let td4 = document.createElement('td');
                            tr.appendChild(td4);
                            let inputElement = document.createElement('input');
                            inputElement.type = 'checkbox';
                            inputElement.name = 'dishId';
                            inputElement.id = parse[i][j].price;
                            inputElement.value = parse[i][j].dishId;
                            td4.appendChild(inputElement);
                        }
                    }
                    let button = document.createElement('button');
                    button.id = 'button';
                    button.innerText = '下单'
                    div.appendChild(button);
                    let a = document.createElement('a');
                    a.innerText = '返回';
                    a.href = 'UserPage.jsp';
                    div.appendChild(a);
                }
            }
        })
        let dishId = [];
        let money = 0;
        setTimeout(function () {
            let dish = document.querySelectorAll('input');
            for (let i = 0; i < dish.length; i++) {
                dish[i].onchange = function () {
                    if (this.checked) {
                        dishId.push(this.value);
                    } else {
                        if (dishId.indexOf(this.value) !== -1) {
                            dishId.splice(dishId.indexOf(this.value), 1);
                        }
                    }
                }
            }
        }, 1000)
        setTimeout(function () {
            $('#button').on('click', function () {
                let s = JSON.stringify(dishId);
                $.ajax({
                    url: "http://localhost:8080/order.do",
                    type: "post",
                    data: {
                        dishId: s,
                        method:'cartOrder'
                    },
                    dataType: "text",
                    success: function (data) {
                        if (data === 'ok') {
                            alert('下单成功!');
                            window.location.href = 'UserPage.jsp';
                        }
                        if (data === 'balance') {
                            alert("余额不足请先充值!");
                            window.location.href = 'Recharge.jsp';
                        }
                        if (data === 'error') {
                            alert('下单失败!');
                        }
                    }
                })
            })
        }, 1000)
    })
</script>
</body>
</html>